package com.songcha.sakura.mvvm.home.cate

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.interfaces.IResult
import com.songcha.library_base.mvvm.base.BaseRefreshLoadMoreViewModel
import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.sakura.bean.ComicListBean
import com.songcha.sakura.config.BaseConfig

class HomeCateViewModel(app: Application) : BaseRefreshLoadMoreViewModel<HomeCateRepository, ComicListBean.ComicBean>(app) {

    //val comicList = MutableLiveData<ComicListBean>()
    //val comicList: MutableLiveData<ComicListBean> = _comicList

    fun getData(index:Int){
        if(index>=3){
            //onLoading()
            getComicList(index)
        }else{
            val cacheTime= SharedPreferenceHelper.getInt("home_cache","cache_time")
            //缓存未过期则取缓存数据
            if((System.currentTimeMillis()/1000)-cacheTime<BaseConfig.HOME_CACHE_UPDATE_TIME){
                handleDataObserver((repository!!).getComicListByDb(index),object :
                    IResult<ComicListBean> {
                    override fun onSuccess(data: ComicListBean) {
                        handleLoadMoreDataListSuccess(data.data)
                        /*handleDataListSuccess(data.data,object : IOnHandleDataListCallback{
                            override fun onUpdate() {
                                comicList.value=data
                            }
                        })*/

                    }
                },true)
            }else{
                getComicList(index)
            }
        }
    }


    private fun getTitleByIndex(index:Int):String{
        var title=""
        when(index){
            0->title="热血"
            1->title="格斗"
            2->title="恋爱"
            3->title="校园"
            4->title="搞笑"
            5->title="萝莉"
            6->title="神魔"
            7->title="机战"
            8->title="科技"
            9->title="青春"
            10->title="魔法"
            11->title="美少女"
            12->title="后宫"
        }
        return title
    }


    fun getComicList(index:Int){
        var title=getTitleByIndex(index-1)
        if(title.isEmpty()) return
        handleApiDataObserver((repository!!).getComicList(title,currentPage),object :
            IApiResult<ComicListBean> {
            override fun onSuccess(bean: ComicListBean) {
                handleDataListSuccess(bean.data)
                /*handleDataListSuccess(bean.data ,object : IOnHandleDataListCallback{
                    override fun onUpdate() {
                        comicList.value=bean
                    }

                    override fun onAppendList() {
                        val tmp=comicList.value
                        tmp!!.data.addAll(bean.data)
                        comicList.value=tmp!!
                    }

                })*/
            }

        },true)

    }


}